﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JibberJabber.LivePreviews;

namespace JibberJabber.PartDesigners
{
    public partial class TextPartDesigner : PartDesigner
    {
        public TextPartDesigner()
        {
            InitializeComponent();
        }

        public override Type[] AllowedChildren
        {
            get
            {
                return new Type[] { };
            }
        }

        public override string StartXml
        {
            get
            {
                return "<part type=\"Text\" speaker=\""
                    + cmbSpeaker.Text + "\"><![CDATA["
                    + textBox1.Text + "]]></part>";
            }
        }

        public override string EndXml
        {
            get
            {
                return "";
            }
        }

        public override string ToString()
        {
            return "[TEXT] " + cmbSpeaker.Text + ": " + textBox1.Text;
        }

        private void cmbSpeaker_TextChanged(object sender, EventArgs e)
        {
            if (this.cmbSpeaker.DropDownStyle == ComboBoxStyle.DropDownList)
            {
                if (this.AssociatedNode != null)
                    Program.NotifyNodeToStringOutputUpdated(this);
                preview();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (this.AssociatedNode != null)
                Program.NotifyNodeToStringOutputUpdated(this);
            preview();
        }

        private void preview()
        {
            if (this.AssociatedNode != null)
            {
                if (this.cmbSpeaker.Text == "")
                {
                    // Shh.
                    this.cmbSpeaker.DropDownStyle = ComboBoxStyle.DropDown;
                    this.cmbSpeaker.Text = "Gilbert";
                }
                LivePreview.Preview(this);
                if (this.cmbSpeaker.DropDownStyle == ComboBoxStyle.DropDown)
                    this.cmbSpeaker.DropDownStyle = ComboBoxStyle.DropDownList;
            }
        }

        private void TextPartDesigner_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
                preview();
        }
    }
}
